﻿using System;
using System.Collections.Generic;

namespace Innovative.ProXr
{
	/// <summary>
	/// Specifies the resolution of the value.
	/// </summary>
	public enum ProXrResolution
	{
		/// <summary>
		/// Specifies and 8-bit resolution which returns value
		/// between 0 and 255
		/// </summary>
		EightBit,
		/// <summary>
		/// Specifies and 8-bit resolution which returns value
		/// between 0 and 1023
		/// </summary>
		TenBit
	}

	/// <summary>
	/// Specifies the relay and relay count
	/// when sending a bank directed on/off command
	/// </summary>
	public class ProXrAnalogInput
	{
		private IRelayDeviceConfiguration _deviceConfiguration = null;
		private RelayController _device = null;

		/// <summary>
		/// Creates an instance of the Relay object using default values.
		/// </summary>
		public ProXrAnalogInput()
		{
		}

		/// <summary>
		/// Creates an instance of the Relay object with a specified device.
		/// </summary>
		/// <param name="deviceConfiguration"></param>
		public ProXrAnalogInput(IRelayDeviceConfiguration deviceConfiguration)
		{
			this.DeviceConfiguration = deviceConfiguration;
		}

		/// <summary>
		/// Gets/sets the IRelayDeviceConfiguration device to manage.
		/// </summary>
		public IRelayDeviceConfiguration DeviceConfiguration
		{
			get
			{
				return _deviceConfiguration;
			}
			set
			{
				_deviceConfiguration = value;
				_device = null;
			}
		}

		public RelayController Device
		{
			get
			{
				if (_device == null)
				{
					_device = new RelayController(this.DeviceConfiguration);
				}

				return _device;
			}
		}

		/// <summary>
		/// Gets the current status of the relay
		/// </summary>
		/// <returns>True if successful, False otherwise.</returns>
		public int ReadValue(int channelId, ProXrResolution resolution)
		{
			int returnValue = 0;

			if (this.DeviceConfiguration != null)
			{
				RelayByte[] response = this.Device.SendCommand(this.GetStatusCommandBytes(channelId, resolution));

				if (resolution == ProXrResolution.EightBit)
				{
					Int32ResponseConverter converter = new Int32ResponseConverter();
					returnValue = ((IResponseConverter<int>)converter).Convert(response);
				}
				else
				{
					DwordResponseConverter converter = new DwordResponseConverter();
					returnValue = ((IResponseConverter<int>)converter).Convert(response);
				}
			}
			else
			{
				throw new DeviceNullException();
			}

			return returnValue;
		}

		internal RelayByte[] GetStatusCommandBytes(int channelId, ProXrResolution resolution)
		{
			RelayByte[] returnValue = new RelayByte[0];

			// ***
			// *** Add the output bytes to a list which will 
			// *** be converted to an array.
			// ***
			List<RelayByte> items = new List<RelayByte>();

			// ***
			// *** Add the command mode command
			// ***
			items.Add(254);

			// ***
			// *** Add the byte specifying the relay
			// ***
			if (resolution == ProXrResolution.EightBit)
			{
				items.Add((byte)(channelId + 150));
			}
			else
			{
				items.Add((byte)(channelId + 158));
			}

			// ***
			// *** Convert the list to an array
			// ***
			returnValue = items.ToArray();

			return returnValue;
		}
	}
}